var key = 0, orRequest = false;
function changeIcon(){
    var token = selectByKey("like_token");
    if(token){
        var imgs = document.getElementsByTagName("a")[1];
        imgs.style.backgroundImage = "url('../../statics/images/index/Group.png')";
        imgs.style.backgroundSize = "23px 23px";
        imgs.style.backgroundPosition = "-1px 0px";
        imgs.style.backgroundRepeat = "no-repeat";
    }
}
function s (select) {
    return document.querySelector(select);
}
function toDetail() {
    var videos = document.querySelectorAll('.video_list');
    for(var j = 0; j < videos.length; j++){
        Tap(videos[j],function(e,dom){
            var videoId = dom.getAttribute('data-video_id');
            window.location.href = 'videocontent.html?list=' + videoId;
        })
    }
}
function ajaxHtml (data, htmlMode, htmlNode) {
    var html = '';
    for(var i = 0; i < data.length; i++){
        var htmlTemp = htmlMode.innerHTML;
        htmlTemp = htmlTemp.replace(/\{\{videoId\}\}/,data[i].id);
        htmlTemp = htmlTemp.replace(/\{\{imageSrc\}\}/,data[i].thumb_pic);
        htmlTemp = htmlTemp.replace(/\{\{videoName\}\}/,data[i].title);
        html += htmlTemp;
    }
    htmlNode.innerHTML += html;
}
function toDetail () {
    var videos = document.querySelectorAll('.video_list');
    for(var j = 0; j < videos.length; j++){
        Tap(videos[j],function(e,dom){
            var videoId = dom.getAttribute('data-video_id');
            window.location.href = 'videocontent.html?list=' + videoId;
        })
    }
}
function search(pageNum) {
    $.ajax({
        url:pathName + "/api/v1/wedding",
        type:"get",
        data:{
            "cid": 0,
            "type": 2,
            "pageNum": pageNum
        },
        success:function(value){
            console.log(value);
            ajaxHtml(value.video.data, s("#mod"), s(".video_wrapper"));
            toDetail();
            if(value.video.current_page < value.video.last_page){
                key = 0;
                orRequest = true;
            }else{
                orRequest = false;
            }
        }
    })
}
window.onload = function () {
    search(1)
    changeIcon();
    toDetail();
    var enter = s('header a:last-child');
    Tap(enter,function(){
        if(selectByKey("like_token")){
            window.location.href = '../personal/individual.html';
        }else{
            window.location.href = '../personal/enter.html';
        }
    });
    var pageNum = 1;
    window.onscroll = function () {
        var t = document.documentElement.scrollTop || document.body.scrollTop;
        //滚动过的高度(屏幕顶到最上面··不包括现在的屏幕高度)
        var y = document.documentElement ? document.documentElement.scrollHeight : document.body.scrollHeight;
        //浏览器滚动条总高
        var w = document.documentElement ? document.documentElement.clientHeight : document.body.clientHeight;
        //屏幕高度
        if (t > 100) {
            document.querySelector('.TOTOP').style.opacity = 1;
        } else {
            document.querySelector('.TOTOP').style.opacity = 0;
        }
        if (t + w > y - 200 && orRequest) {
            if (key == 0) {
                key = 1;
                pageNum++;
                search(pageNum);
            }
        }
    };
}